package com.kabuda.zhugehot200;

/**
 * @author kabuda
 * @date 2025-03-15 19:43
 * @description
 */
public class t628_三个数的最大乘积 {
    public static void main(String[] args) {
        System.out.println(maximumProduct(new int[]{1, 2, 3, 4, 5, -6}));
    }

    /**
     * 线性扫描
     *
     * @param nums
     * @return
     */
    public static int maximumProduct(int[] nums) {
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
        for (int num : nums) {
            if (num < min1) {
                min2 = min1;
                min1 = num;
            } else if (num < min2) {
                min2 = num;
            }

            if (num > max1) {
                max3 = max2;
                max2 = max1;
                max1 = num;
            } else if (num > max2) {
                max3 = max2;
                max2 = num;
            } else if (num > max3) {
                max3 = num;
            }
        }


        return Math.max(min1 * min2 * max1, max1 * max2 * max3);
    }
}
